import type { Request, Response } from 'express';


function businessFinance(req: Request, res: Response) {
  const queryParams = req.query;
  // 示例：访问 http://xxx/gtezid?name=test&age=18 时，queryParams 为 { name: 'test', age: '18' }

  // 2. 获取 POST/PUT 等请求的请求体（JSON 数据）
  const bodyData = req.body;
  // 示例：客户端 POST 发送 { "id": 1, "status": "active" } 时，bodyData 为对应的对象

  // 3. 获取 URL 路径参数（如 /gtezid/:id 中的 id）
  const params = req.params;
  // 若路由定义为 '/gtezid/:id'，访问 /gtezid/123 时，params 为 { id: '123' }

  // 可以根据需要处理这些参数
  console.log('查询参数:', queryParams);
  console.log('请求体:', bodyData);
  console.log('路径参数:', params);

  // 生成指定范围的随机整数
  const randInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
  const genInvoiceAmount = () => randInt(0, 10000); // 金额 0-10000 随机整数
  const genInvoiceNumber = () => randInt(1000000000, 9999999999); // 10位随机数
  const genInvoiceDocumentNumber = () => randInt(100000, 999999); // 6位随机数

  if (queryParams) {
    res.send({
      data: {
        message: 'Ok',
        total: 1,
        code: 200,
        list: [{
          index: 1,
          title: '鼎盛商贸有限公司',
          labels: [{ name: 'Bug', color: 'red' }, { name: '紧急', color: 'orange' }],
          state: 'open',
          comments: 3,
          id: 1,
          key: 1,
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'Y',
          stateType: 'open',
          titleName: '用户登录异常排查',
          created_at: '2023-05-12',
          url: 'https://example.com/item/1',
        },
        {
          index: 2,
          title: '宏图伟业销售中心',
          labels: [{ name: '优化', color: 'blue' }],
          state: 'processing',
          comments: 5,
          id: 2,
          key: 2,
          invoiceAmount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          sellerAccount: genInvoiceAmount(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'Y',
          stateType: 'processing',
          titleName: '宏图伟业销售中心',
          created_at: '2023-06-20',
          url: 'https://example.com/item/2',
        },
        {
          index: 3,
          title: '诚信达供应链管理有限公司',
          labels: [{ name: 'UI', color: 'purple' }, { name: '移动端', color: 'green' }],
          state: 'open',
          comments: 2,
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
          id: 3,
          key: 3,
          stateType: 'open',
          titleName: '诚信达供应链管理有限公司',
          created_at: '2023-04-05',
          url: 'https://example.com/item/3',
        },
        {
          index: 4,
          title: '恒通九洲贸易有限公司',
          labels: [{ name: '新功能', color: 'cyan' }],
          state: 'closed',
          comments: 8,
          id: 4,
          key: 4,
          stateType: 'closed',
          titleName: '恒通九洲贸易有限公司',
          created_at: '2023-03-18',
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
          url: 'https://example.com/item/4',
        },
        {
          index: 5,
          title: '瑞丰泰科贸有限公司',
          labels: [{ name: 'Bug', color: 'red' }, { name: '支付', color: 'gold' }],
          state: 'processing',
          comments: 12,
          id: 5,
          key: 5,
          stateType: 'processing',
          titleName: '瑞丰泰科贸有限公司',
          created_at: '2023-07-02',
          url: 'https://example.com/item/5',
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
        },
        {
          index: 6,
          title: '聚鑫源百货销售中心',
          labels: [{ name: '文档', color: 'grey' }],
          state: 'open',
          comments: 1,
          id: 6,
          key: 6,
          stateType: 'open',
          titleName: '聚鑫源百货销售中心',
          created_at: '2023-05-28',
          url: 'https://example.com/item/6',
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
        },
        {
          index: 7,
          title: '华茂恒业商贸集团',
          labels: [{ name: '后端', color: 'geekblue' }, { name: '优化', color: 'blue' }],
          state: 'processing',
          comments: 6,
          id: 7,
          key: 7,
          stateType: 'processing',
          titleName: '华茂恒业商贸集团',
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
          created_at: '2023-06-15',
          url: 'https://example.com/item/7',
        },
        {
          index: 8,
          title: 'xxxxx商贸集团',
          labels: [{ name: '产品', color: 'magenta' }],
          state: 'open',
          comments: 9,
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
          id: 8,
          key: 8,
          stateType: 'open',
          titleName: 'xxxxx商贸集团',
          created_at: '2023-04-30',
          url: 'https://example.com/item/8',
        },
        {
          index: 9,
          title: 'xxxxx商贸集团',
          labels: [{ name: 'Bug', color: 'red' }, { name: '搜索', color: 'lime' }],
          state: 'closed',
          comments: 4,
          invoiceAmount: genInvoiceAmount(),
          sellerAccount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
          id: 9,
          key: 9,
          stateType: 'closed',
          titleName: 'xxxxx商贸集团',
          created_at: '2023-03-08',
          url: 'https://example.com/item/9',
        },
        {
          index: 10,
          title: 'xxxxx商贸集团',
          labels: [{ name: '前端', color: 'volcano' }, { name: '活动', color: 'pink' }],
          state: 'open',
          comments: 7,
          id: 10,
          invoiceAmount: genInvoiceAmount(),
          invoiceNumber: genInvoiceNumber(),
          sellerAccount: genInvoiceAmount(),
          invoiceDocumentNumber: genInvoiceDocumentNumber(),
          isControlInvoicing: 'N',
          key: 10,
          stateType: 'open',
          titleName: 'xxxxx商贸集团',
          created_at: '2023-07-10',
          url: 'https://example.com/item/10',
        },],
      }


    });
  }

}
function gtezid(_: Request, res: Response) {
  res.send({ data: [{ index: 1 }] });
}
function basicForm(_: Request, res: Response) {
  res.send({ data: [{ index: 1 }] });
}
function businessFinanceIndexList(_: Request, res: Response) {
  // 生成随机数据的辅助函数
  const randInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
  const randFloat = (min: number, max: number) => (Math.random() * (max - min) + min).toFixed(2);
  const randDate = (start: Date, end: Date) => {
    const date = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
    return date.toISOString().split('T')[0];
  };
  const randItem = <T>(arr: T[]): T => arr[Math.floor(Math.random() * arr.length)];

  // 基础数据池
  const operators = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十'];
  const departments = ['财务部', '销售部', '采购部', '运营部', '市场部', '行政部'];
  const customerNames = [
    '鼎盛商贸有限公司',
    '宏图伟业销售中心',
    '诚信达供应链管理有限公司',
    '恒通九洲贸易有限公司',
    '瑞丰泰科贸有限公司',
    '聚鑫源百货销售中心',
    '华茂恒业商贸集团',
    '永信达贸易公司',
    '锦绣前程企业集团',
    '盛世华章商贸中心'
  ];
  const professionalTypes = [
    '金融服务',
    '信息技术',
    '咨询服务',
    '贸易零售',
    '制造业',
    '建筑业',
    '医疗健康',
    '教育培训',
    '文化传媒',
    '物流运输'
  ];
  const salesTypes = ['直销', '代理', '经销', '分销'];
  const processStatuses: ('pending' | 'processing' | 'completed' | 'rejected')[] = ['pending', 'processing', 'completed', 'rejected'];
  const invoiceTypes: ('normal' | 'special')[] = ['normal', 'special'];
  const invoiceCategories: ('vat' | 'ordinary')[] = ['vat', 'ordinary'];

  // 生成10条随机数据
  const list = Array.from({ length: 10 }, (_, index) => {
    const totalAmountWithTax = Number(randFloat(10000, 500000));
    const taxRate = 0.13; // 假设税率13%
    const totalAmountWithoutTax = Number((totalAmountWithTax / (1 + taxRate)).toFixed(2));
    const taxAmount = Number((totalAmountWithTax - totalAmountWithoutTax).toFixed(2));

    return {
      id: index + 1,
      index: index + 1,
      invoiceNo: `INV${new Date().getFullYear()}${String(randInt(100000, 999999)).padStart(6, '0')}`,
      processStatus: randItem(processStatuses),
      operator: randItem(operators),
      operateDate: randDate(new Date(2024, 0, 1), new Date()),
      operateDept: randItem(departments),
      invoiceType: randItem(invoiceTypes),
      customerName: randItem(customerNames),
      totalAmountWithTax,
      totalAmountWithoutTax,
      taxAmount,
      invoiceCategory: randItem(invoiceCategories),
      invoiceNumber: String(randInt(10000000, 99999999)),
      invoiceCode: String(randInt(1000000000, 9999999999)),
      issueDate: randDate(new Date(2024, 0, 1), new Date()),
      postingDate: randDate(new Date(2024, 0, 1), new Date()),
      sapVoucherNo: `SAP${randInt(1000000, 9999999)}`,
      totalInvoicedWithTax: Number(randFloat(50000, 1000000)),
      totalInvoicedWithoutTax: Number(randFloat(40000, 900000)),
      totalTax: Number(randFloat(5000, 100000)),
      professionalType: randItem(professionalTypes),
      writtenOffAmount: Number(randFloat(0, 50000)),
      unwrittenOffAmount: Number(randFloat(0, 50000)),
      invoiceUnit: randItem(customerNames),
      salesType: randItem(salesTypes),
    };
  });

  res.send({
    data: {
      message: 'Ok',
      total: 10,
      code: 200,
      list,
    },
  });
}
function businessFinanceSubmitForm(_: Request, res: Response) {
  res.send({
    code: 200,
    message: '提交成功',
  });
}
export default {
  'POST  /api/businessFinance': businessFinance,
  'POST  /api/gtezid': gtezid,
  'POST  /api/basicForm': basicForm,
  'POST  /api/businessFinanceSubmitForm': businessFinanceSubmitForm,
  'POST  /api/businessFinanceIndexList': businessFinanceIndexList
};

